package com.md.album.presenter

import com.md.album.AlbumModel
import com.md.album.contracts.SelfAlbumContract
import com.md.album.event.SelfAlbumEventModel
import com.md.basedpc.base.BasePresenter
import com.md.network.api.Album

class SelfAlbumPresenter: BasePresenter<SelfAlbumContract.View>(), SelfAlbumContract.Presenter{

    private val model: AlbumModel by lazy {
        AlbumModel()
    }

    override fun getAlbum(albumId: Int) {
        checkViewAttached()
        val disposable =model.getAlbumInfo(albumId).subscribe({
            albumInfo ->
            mRootView?.apply{
                wrapAudio(albumInfo)
                showAlbumInfo(albumInfo)

            }
        },{ throwable ->
            mRootView?.apply {
                showError(throwable.toString())
            }
        })

        disposable?.let { addSubscription(it) }
    }

    private fun wrapAudio(album: Album?){
        if (album !=null && album?.audios.isNotEmpty()){
            for (ad in album.audios){
                ad.albumId = album.id
                ad.albumAuthor = album.author?:"未知作者"
                ad.albumName = album.name
                ad.albumDes = album.desc
            }
        }
    }

}